package com.cmic.rcs.emoji.config;

import com.cmic.origin.login.grpc.LoginServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@SuppressWarnings("unused")
@Configuration
public class LoginGrpcConfig {
    @Value("${grpc.client.login-grpc-server.host}")
    private String loginGrpcServerHost;

    @Value("${grpc.client.login-grpc-server.port}")
    private Integer loginGrpcServerPort;

    private LoginServiceGrpc.LoginServiceBlockingStub loginServiceGrpc;

    @Bean
    public LoginServiceGrpc.LoginServiceBlockingStub loginServiceGrpc() {
        try {
            SslContextBuilder builder = GrpcSslContexts.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE);
            ManagedChannel channel = NettyChannelBuilder.forAddress(loginGrpcServerHost, loginGrpcServerPort)
                    .sslContext(builder.build())
                    .build();
            return LoginServiceGrpc.newBlockingStub(channel);
        } catch (Exception e) {
            log.error("消息grpc初始化异常: {}", e.toString(), e);
        }
        return null;
    }
}
